<template>
  <div style="width: 100%;padding: 0 20px;">
    <div
      style="display: flex; justify-content: space-between; align-items: center; margin-top: 20px;margin-bottom: 20px;margin-right: 50px;">
      <h3 style="margin: 10; ">{{ $route.query.id }}班学生</h3>
      <div style="display: flex;">
        <el-input placeholder="学生搜索" style="width: 300px;" v-model="name"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
      </div>
    </div>
    <!--不使用搜索框的时候-->
    <el-table v-if="!name" :data="pagination" border style="width: 100%;" height="550px" @row-click="toStudentMessage">
      <el-table-column prop="userId" label="学生ID" fixed width="200px">
      </el-table-column>
      <el-table-column prop="levelId" label="等级" width="150px">
      </el-table-column>
      <el-table-column prop="noteNum" label="笔记数" width="150px">
      </el-table-column>
      <el-table-column prop="learnedNum" label="已学课时数" width="150px">
      </el-table-column>
      <el-table-column label="操作" width="auto" prop="studentId">
        <el-button>查看详情</el-button>
      </el-table-column>
    </el-table>

    <!--使用搜索框的时候-->
    <el-table v-else :data="searchlist" style="width: 100%;" height="550px" @row-click="toStudentMessage">
      <el-table-column prop="userId" label="学生ID" fixed width="200px">
      </el-table-column>
      <el-table-column prop="levelId" label="等级" width="150px">
      </el-table-column>
      <el-table-column prop="noteNum" label="笔记数" width="150px">
      </el-table-column>
      <el-table-column prop="learnedNum" label="已学课时数" width="150px">
      </el-table-column>
      <el-table-column label="操作" width="auto" prop="userId">
        <el-button>查看详情</el-button>
      </el-table-column>
    </el-table>
    <!--页面跳转,分页器-->
    <div class="block"
      style="display: flex; justify-content: flex-end; align-items: flex-end; flex ; bottom: 20px; right: 20px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalItems">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      studentid: 0,
      start: 0,
      end: 10,
      name: '',
      list: [],
      searchlist: [],
      pagination: [],
      currentPage: 1,
      pageSize: 6, // 每页显示的条目数量
      totalItems: 50, // 数据库中的总条目数量
      pageSizes: [6, 8, 10, 20] // 每页显示条目数量的选项
    }
  },

  methods: {
    toStudentMessage (row, column, event) {
      // 跳转路由，携带参数id
      console.log(row)
      this.$router.push({
        path: '/studentmessage',
        query: {
          id: row.userId
        }
      })
    },
    search () {
      // 查询和搜索框id一样的数据保留存入searchlist
      this.searchlist = this.list.filter(item => {
        return item.userId === parseInt(this.name)
      })
    },
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.fetchData() // 更新每页显示的条目数量后重新请求数据
    },
    handleCurrentChange (newPage) {
      this.currentPage = newPage
      this.fetchData() // 更新当前页数后重新请求数据
    },
    fetchData () {
      this.start = (this.currentPage - 1) * this.pageSize
      this.end = this.start + this.pageSize
      this.pagination = this.list.slice(this.start, this.end)
    },
    async getData () {
      const id = this.$route.query.id
      const res = await axios.get('http://127.0.0.1:8000/api/classMangement_system/classroom_table/', {
        params: {
          classroomId: id
        }
      }
      )
      // 接收数据
      this.list = res.data.msg.data
      // 开始分页处理
      this.pagination = this.list.slice(0, this.pageSize)
      this.totalItems = this.list.length
    }
  },
  created () {
    this.getData()
  }
}
</script>

<style>

</style>
